﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;

namespace IkeCode.WP7.HQReader.Services
{
    /// <summary>
    /// Loading Control model
    /// </summary>
    public class IcLoadingViewModel
    {
        private bool _isLoading = true;
        /// <summary>
        /// Visibility of Loading control.
        /// </summary>
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                if (_isLoading == value)
                    return;
                _isLoading = value;
            }
        }

        private string _loadingMessage = "Loading...";
        /// <summary>
        /// Loading message
        /// </summary>
        public string LoadingMessage
        {
            get { return _loadingMessage; }
            set
            {
                if (_loadingMessage == value)
                    return;
                _loadingMessage = value;
            }
        }

        private bool _showLoadingMessage = true;
        /// <summary>
        /// Show loading message flag
        /// </summary>
        public bool ShowLoadingMessage
        {
            get { return _showLoadingMessage; }
            set
            {
                if (_showLoadingMessage == value)
                    return;
                _showLoadingMessage = value;
            }
        }

        private string _errorMessage = "Error occurred.";
        /// <summary>
        /// Error message
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                if (_errorMessage == value)
                    return;
                _errorMessage = value;
            }
        }

        private bool _showErrorMessage;
        /// <summary>
        /// Show error message flag
        /// </summary>
        public bool ShowErrorMessage
        {
            get { return _showErrorMessage; }
            set
            {
                if (_showErrorMessage == value)
                    return;
                _showErrorMessage = value;
            }
        }

        private bool _showReloadButton;
        /// <summary>
        /// Show reload button flag
        /// </summary>
        public bool ShowReloadButton
        {
            get { return _showReloadButton; }
            set
            {
                if (_showReloadButton == value)
                    return;
                _showReloadButton = value;
            }
        }

        private ICommand _reloadCommand;
        /// <summary>
        /// Reload button command
        /// </summary>
        public ICommand ReloadCommand
        {
            get { return _reloadCommand; }
            set
            {
                if (_reloadCommand == value)
                    return;
                _reloadCommand = value;
            }
        }
    }

    /// <summary>
    /// Loading model class
    /// </summary>
    public class IcLoadingService
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public IcLoadingService()
        {
        }

        /// <summary>
        /// Switch loading block visibility
        /// </summary>
        /// <param name="isVisible">Set control visibility</param>
        /// <param name="showLoadingMessage">Show loading message flag</param>
        /// <param name="loadingMessage">Loading message</param>
        public void LoadingSwitch(bool isVisible, bool showLoadingMessage = true, string loadingMessage = "")
        {
            var viewModel = new IcLoadingViewModel();

            viewModel.IsLoading = isVisible;
            if (!string.IsNullOrWhiteSpace(loadingMessage))
            {
                viewModel.LoadingMessage = loadingMessage;
            }
            viewModel.ShowLoadingMessage = isVisible && showLoadingMessage;
            viewModel.ShowErrorMessage = false;
            viewModel.ReloadCommand = null;
            viewModel.ShowReloadButton = false;
            Messenger.Default.Send<IcLoadingViewModel>(viewModel, "LoadingSwitch");
        }

        /// <summary>
        /// Switch error block visibility
        /// </summary>
        /// <param name="errorMessage">Error message</param>
        /// <param name="reloadCommand">Reload button command, the button just appear if has value</param>
        public void ErrorSwitch(string errorMessage = "", ICommand reloadCommand = null)
        {
            var viewModel = new IcLoadingViewModel();

            viewModel.IsLoading = false;
            viewModel.ShowLoadingMessage = false;
            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                viewModel.ErrorMessage = errorMessage;
            }
            viewModel.ShowErrorMessage = true;
            this.SetReloadCommand(reloadCommand);
            Messenger.Default.Send<IcLoadingViewModel>(viewModel, "LoadingSwitch");
        }

        /// <summary>
        /// Set reload button command, normally called at last step after error occur
        /// </summary>
        /// <param name="command">Command</param>
        /// <param name="showReloadCommand">Show reload button flag</param>
        public void SetReloadCommand(ICommand command, bool showReloadCommand = true)
        {
            var viewModel = new IcLoadingViewModel();

            viewModel.ReloadCommand = command;
            viewModel.ShowReloadButton = showReloadCommand && command != null;
            Messenger.Default.Send<IcLoadingViewModel>(viewModel, "LoadingSwitch");
        }
    }
}
